package com.sky.myapplication.kotlin.kotlinActivity

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.provider.ContactsContract
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.sky.myapplication.R

/**
 * Created by Min
 * Time 2021/6/19
 * Desc Kotlin编写同样的类
 */

//kotlin中没有extends，implements，继承直接类名（），实现接口直接接口名 https://blog.csdn.net/qq_33210042/article/details/90044334
class ContentProviderKotlinActivity: AppCompatActivity(){
    //lateinit属性 https://zhuanlan.zhihu.com/p/31297995/
    lateinit var tv_log: TextView
    lateinit var data: HashMap<String, String>
    var dataList: ArrayList<Map<String, String>> = ArrayList<Map<String, String>>()

    var log: String? = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_content_provider)

        initView()
        handlePermisson()
    }

    fun initView(){
        tv_log = findViewById(R.id.tv_log)
    }

    fun initData(){
//        var cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null)
//        while (cursor!!.moveToNext()) {
//            var idFileldIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID)
//            var id = cursor.getInt(idFileldIndex)//根据列名取得该联系人的id
//            var nameFieldIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)
//            var name = cursor.getString(nameFieldIndex)//根据列名取得该联系人的name
//            var numCountFieldIndex = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)
//            var numCount = cursor.getInt(numCountFieldIndex)
//
//            var phoneNumber = ""
//            if (numCount > 0){
//                var phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", arrayOf(id.toString()),null)
//
//                if (phoneCursor!!.moveToFirst()) {
//                    var numFiledIndex = phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
//                    phoneNumber = phoneCursor.getString(numFiledIndex)
//                }
//            }
//            data = HashMap<String, String>()
//            data.put("name", name)
//            data.put("phoneNumber", phoneNumber)
//            dataList.add(data)
//        }
//        Log.i("data", dataList.toString())
//
//        for (arg in dataList){
//            log += "name: " + arg.get("name") + " phone: " + arg.get("phoneNumber") + "\n"
//        }
//
//        tv_log.text = log
    }

    fun handlePermisson(){
        // 需要动态申请的权限
        val permission = Manifest.permission.READ_CONTACTS
        //查看是否已有权限
        val checkSelfPermission = ActivityCompat.checkSelfPermission(this,permission)

        if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
//            没有拿到权限  是否需要在第二次请求权限的情况下
//             先自定义弹框说明 同意后在请求系统权限(就是是否需要自定义DialogActivity)
            myRequestPermission()
        } else {
            initData()
        }

    }

    private fun myRequestPermission() {
        //可以添加多个权限申请
        val permissions = arrayOf(Manifest.permission.READ_CONTACTS)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(permissions,1)
        }
    }

    /***
     * 权限请求结果  在Activity 重新这个方法 得到获取权限的结果  可以返回多个结果
     */
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        //是否获取到权限
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
            if (permissions[0].equals(Manifest.permission.READ_CONTACTS)){
                initData()
            }
        }

    }
}